package com.wit.health;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import com.wit.health.fragment.DeviceFragment;
import com.wit.health.fragment.DiscoverFragment;
import com.wit.health.fragment.HomeFragment;
import com.wit.health.fragment.MeFragment;


/**
* 主界面（含底部导航栏）
*
* @Author liaozhaoping
* @Date 2024/5/20 10:08
*/

public class MainActivity2 extends AppCompatActivity {
    private Fragment homeFragment;
    private Fragment discoverFragment;
    private Fragment meFragment;

    private DeviceFragment deviceFragment;

    String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        navView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);

        username = getIntent().getStringExtra("USERNAME_KEY");



        homeFragment = new HomeFragment();
        discoverFragment = new DiscoverFragment();
        meFragment = new MeFragment();
        deviceFragment = new DeviceFragment();

        // 传递给 Fragment
        passDataToFragments(username);

        if(savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, homeFragment)
                    .add(R.id.fragment_container, discoverFragment)
                    .add(R.id.fragment_container, meFragment)
                    .add(R.id.fragment_container, deviceFragment)
                    .hide(discoverFragment)
                    .hide(meFragment)
                    .hide(homeFragment)
                    .show(deviceFragment)
                    .commit();
        }


    }

    private void passDataToFragments(String username) {
        Bundle bundle = new Bundle();
        bundle.putString("USERNAME_KEY", username);
        homeFragment.setArguments(bundle);
        deviceFragment.setArguments(bundle);
        meFragment.setArguments(bundle);
        deviceFragment.setArguments(bundle);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    switch (item.getItemId()) {
                        case R.id.navigation_home:
                            getSupportFragmentManager().beginTransaction()
                                    .hide(discoverFragment)
                                    .hide(meFragment)
                                    .hide(deviceFragment)
                                    .show(homeFragment)
                                    .commit();
                            return true;
                        case R.id.navigation_discover:
                            getSupportFragmentManager().beginTransaction()
                                    .hide(homeFragment)
                                    .hide(meFragment)
                                    .hide(deviceFragment)
                                    .show(discoverFragment)
                                    .commit();
                            return true;
                        case R.id.navigation_me:
                            getSupportFragmentManager().beginTransaction()
                                    .hide(homeFragment)
                                    .hide(discoverFragment)
                                    .hide(deviceFragment)
                                    .show(meFragment)
                                    .commit();
                            return true;
                        case R.id.navigation_device:
                            getSupportFragmentManager().beginTransaction()
                                    .hide(homeFragment)
                                    .hide(discoverFragment)
                                    .hide(meFragment)
                                    .show(deviceFragment)
                                    .commit();
                            return true;
                    }
                    return false;

                }
            };
}